package com.huijutao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.huijutao.NetUrl;
import com.huijutao.activityContent.FindActivity;
import com.huijutao.MyApp;
import com.huijutao.R;
import com.huijutao.beans.FindItem;
import com.huijutao.callback.ABSFragment;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现页面Fragment
 * Created by 兴盛 on 2015/4/24.
 */
public class FMFind extends ABSFragment {

    private PullToRefreshGridView mPgv;
    private GvAdapter mGvAdapter;
    private int mPageNow;
    private int mType;

    /**
     * 根据类别修改显示内容，配合SlidingMenu
     * @param type 0-7
     */
    @Override
    public void setType(int type) {
        this.mType = type;
        load(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_findgroup, container, false);
        mPgv = (PullToRefreshGridView) view.findViewById(R.id.pgv);
        //设置刷新样式
        RefreshSetting.setPullDownAndUp(mPgv);
        GridView gv = mPgv.getRefreshableView();
        //设置适配器
        mGvAdapter = new GvAdapter();
        gv.setAdapter(mGvAdapter);
        //默认为刷新
        load(true);
        mPgv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                load(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                load(false);
            }
        });
        mPgv.setPullToRefreshOverScrollEnabled(true);
        return view;
    }

    /**
     * 加载数据，可以判断是否是刷新还是加载
     *
     * @param refresh true，刷新；false，加载
     */
    private void load(boolean refresh) {
        if (refresh) {
            mPageNow = 0;
            mGvAdapter.clear();
        }
        MyApp.httpUtils.send(HttpRequest.HttpMethod.GET,
                NetUrl.getFindUrl(++mPageNow, mType),
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                        mGvAdapter.add(FindItem.getListFromJsonStr(objectResponseInfo.result));
                        mGvAdapter.notifyDataSetChanged();
                        mPgv.onRefreshComplete();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        mPgv.onRefreshComplete();
                    }
                }
        );
    }


    class GvAdapter extends BaseAdapter {
        private List<FindItem> list;

        GvAdapter() {
            list = new ArrayList<>();
        }

        public void clear() {
            list.clear();
        }

        public void add(List<FindItem> listNew) {
            if(listNew==null){
                return;
            }
            list.addAll(listNew);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            GvHolder holder;
            if (convertView == null) {
                holder = new GvHolder();
                convertView = getActivity().getLayoutInflater().inflate(R.layout.item_find, null);
                holder.img = (ImageView) convertView.findViewById(R.id.img);
                holder.txt_title = (TextView) convertView.findViewById(R.id.txt_title);
                holder.txt_price = (TextView) convertView.findViewById(R.id.txt_price);
                convertView.setTag(holder);
            } else {
                holder = (GvHolder) convertView.getTag();
            }
            final FindItem findItem = list.get(position);
            MyApp.mImageXutils.display(holder.img, findItem.getImg_path480());
            holder.txt_title.setText(findItem.getTitle());
            holder.txt_price.setText("¥" + findItem.getPrice());
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(getActivity(), FindActivity.class);
                    i.putExtra(FindActivity.DATA, findItem);
                    startActivity(i);
                }
            });
            return convertView;
        }
    }

    class GvHolder {
        ImageView img;
        TextView txt_title;
        TextView txt_price;
    }
}
